﻿using System.Web.Mvc;
using Orchard;
using Orchard.Comments.Models;
using Orchard.Comments.Services;
using Orchard.Comments.ViewModels;
using Orchard.Environment.Extensions;
using Orchard.Localization;
using Orchard.Mvc.Filters;
using Orchard.UI.Notify;

namespace Dndz.Industries.Services
{
    [OrchardFeature("Dndz.Industries.Captcha.Comments")]
    [OrchardSuppressDependency("Orchard.Comments.Services.AkismetCommentValidator")]
    public class CaptchaCommentValidator : ICommentValidator
    {
        private readonly INotifier _notifer;
        private readonly ICaptchaService _captchaService;
        private readonly IOrchardServices _orchardServices;

        public CaptchaCommentValidator(INotifier notifier, 
            ICaptchaService captcha, IOrchardServices services)
        {
            _notifer = notifier;
            _captchaService = captcha;
            _orchardServices = services;
            T = NullLocalizer.Instance;
        }

        public Localizer T { get; set; }

        public bool ValidateComment(CommentPart commentPart)
        {
            if (!_captchaService.IsCaptchaValid())
            {
                _notifer.Error(T("Please enter correct Values exactly as you can see in the Picture!"));

                var httpRequest = _orchardServices.WorkContext.HttpContext.Request;

                var viewModel = (CommentsCreateViewModel)new DefaultModelBinder()
                    .BindModel(new ControllerContext(), 
                        new ModelBindingContext
                        {
                            ValueProvider = new NameValueCollectionValueProvider(httpRequest.Params, null),
                            ModelMetadata = ModelMetadataProviders
                                .Current.GetMetadataForType(null, typeof(CommentsCreateViewModel)),
                        }
                );

                var mediator = _orchardServices.WorkContext.Resolve<ICaptchaCommentTempData>();
                mediator.CreateCommentContext = viewModel;

                return false;
            }

            return true;
        }
    }

    /* Context storage during request  */

    public interface ICaptchaCommentTempData : IDependency
    {
        CommentsCreateViewModel CreateCommentContext { get; set; }
    }

    [OrchardFeature("Dndz.Industries.Captcha.Comments")]
    public class CaptchaCommentTempDataImpl : ICaptchaCommentTempData
    {
        public CommentsCreateViewModel CreateCommentContext { get; set; }
    }

    [OrchardFeature("Dndz.Industries.Captcha.Comments")]
    public class CaptchaCommentValidatorFilter : FilterProvider, IActionFilter
    {
        private readonly ICaptchaCommentTempData _tempDataMediator;

        public CaptchaCommentValidatorFilter(ICaptchaCommentTempData tempDataMediator)
        {
            _tempDataMediator = tempDataMediator;
        }

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            // don't touch temp data if there's no work to perform
            if (_tempDataMediator.CreateCommentContext == null)
                return;

            var tempData = filterContext.Controller.TempData;

            tempData["Validator.CreateCommentContext"] = _tempDataMediator.CreateCommentContext;
        }

        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
        }
    }
}
